package xuperchain

import (
	"encoding/json"
	"fmt"

	"github.com/xuperchain/xuper-sdk-go/v2/account"
	"github.com/xuperchain/xuper-sdk-go/v2/xuper"
)

func GetFile(paperName string) (string, string, error) {

	account, err := account.GetAccountFromFile(path, pwd)
	if err != nil {
		fmt.Printf("GetAccountFromFile err: %v\n", err)
		return "", "", err
	}

	err = account.SetContractAccount(contractAccount)
	if err != nil {
		return "", "", err
	}

	xchainClient, err := xuper.New(nodeAddress)
	if err != nil {
		return "", "", err
	}

	contractName := "xc_filestorage8"

	argsGetFile := map[string]string{"_name": paperName}

	txGetFile, err := xchainClient.QueryEVMContract(account, contractName, "getFile", argsGetFile)
	if err != nil {
		return "", "", err
	}
	var response []struct {
		Name     string `json:"0"`
		Abstract string `json:"1"`
	}
	err = json.Unmarshal([]byte(txGetFile.ContractResponse.Body), &response)
	if err != nil {
		return "", "", err
	}
	return response[0].Name, response[1].Abstract, nil
}
